package com.yituo.mjworld.mvc.action.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.annotation.Check;
import com.yituo.mjworld.core.annotation.Rule;
import com.yituo.mjworld.core.web.core.MyHttpRequest;
import com.yituo.mjworld.core.web.core.MyHttpResponse;
import com.yituo.mjworld.mvc.action.IBaseAction;
import com.yituo.mjworld.mvc.entity.web.ProductProps;
import com.yituo.mjworld.mvc.service.impl.ProductPropsServiceImpl;
import com.yituo.mjworld.mvc.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 产品的属性Action
 *
 * @author xq
 */
@Component
public class ProdPropsAction implements IBaseAction {

  @Autowired
  ProductPropsServiceImpl productPropsService;
  @Autowired
  ProductServiceImpl productService;

  /**
   * 获取产品的某个属性
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Rule(name = "id", name_zn = "模版ID", min = "0")
  public void getTemplate(MyHttpRequest req, MyHttpResponse res) throws IOException {
    int id = Integer.parseInt(req.getParameter("id"));
    if (!productService.templateIsExits(id)) {
      res.failed("产品不存在!");
      return;
    }
    res.success(productPropsService.getTemplate(id));
  }

  /**
   * 获取某个商品所有的属性
   *
   * @param req
   * @param res
   * @throws IOException
   * @deprecated 该接口的定义不再适用于当前概念, 因此废弃
   */
  @Override
  @Rule(name = "productId", name_zn = "产品ID", min = "0")
  public void getList(MyHttpRequest req, MyHttpResponse res) throws IOException {
    int productId = Integer.parseInt(req.getParameter("productId"));
    if (!productService.productIsExits(productId)) {
      res.failed("产品不存在!");
      return;
    }
    res.success(productPropsService.getList(productId));
  }

  /**
   * 删除一个产品的属性
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Override
  @Rule(name = "id", name_zn = "欲删除的模版ID")
  public void delete(MyHttpRequest req, MyHttpResponse res) throws IOException {
    int productId = Integer.parseInt(req.getParameter("id"));
    res.success(productPropsService.delete(productId));
  }

  /**
   * 保存或者更新
   *
   * @param req
   * @param res
   * @throws IOException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  @Check(form = ProductProps.class)
  @Override
  public void saveOrUpdate(MyHttpRequest req, MyHttpResponse res) throws IOException, InvocationTargetException, IllegalAccessException {
    String templateStr = req.getParameter("template");
    JSONArray templateJson = null;
    try {
      templateJson = (JSONArray) JSONObject.parse(templateStr);
    } catch (Exception e) {
      System.err.println(e);
      System.err.println("该JSON模版无法识别!");
      res.failedFor302("该JSON模版无法识别!");
      return;
    }
    ProductProps productProp = (ProductProps) req.getForm();
    res.setResult(productPropsService.saveOrUpdate(productProp));
  }
}
